package ObserverMode

import scala.collection.mutable.ListBuffer

class WeatherDataSt extends Subject {
  private var tmeperature: Float = _
  private var pressure: Float = _
  private var humidity: Float = _
  //集合,用于管理所有的观察者
  private var mObervers: ListBuffer[Observer] = ListBuffer()

  def getTemperature() = this.tmeperature

  def getPressure() = this.pressure

  def getHumity() = this.humidity

  def setData(mt: Float, mP: Float, mH: Float): Unit = {
    this.tmeperature = mt
    this.pressure = mP
    this.humidity = mH
    //要求修改公告板的最新数据
    dataChange()
  }

  def dataChange(): Unit = {
    notifyObservers()
  }


  override def registerObderver(o: Observer): Unit = {
    this.mObervers.append(o)
  }

  override def removeObserver(o: Observer): Unit = {
    if (mObervers.contains(o))
      mObervers -= o
  }

  override def notifyObservers(): Unit = {
    mObervers.foreach(o => o.update(this.tmeperature, this.pressure, this.humidity))
  }


}